#
# 青少年抑郁症筛查主模块
#


import datetime

from rasa_sdk.executor import CollectingDispatcher
from typing import Text, Dict, Any, List

from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet, FollowupAction, SessionStarted
from actions import common
from actions import heart_end
import requests
import time


# def save(dispatcher,tracker):
#     try:
#         userid = tracker.current_state()['sender_id']
#         a = tracker.current_state()['events'][-2]
#         Lastline = tracker.current_state()['events'][-4]['text']
#         userintent = a['parse_data']['intent']['name']
#         userconfidence = a['parse_data']['intent']['confidence']
#         useranswer = a['text']
#         x = "Insert into `user_heart_action`(id,user_id,boot_now,user_intent,user_action,confidence) VALUES('" + common.getuid() + "','" + userid + "','" + Lastline + "','" + userintent + "','" + useranswer + "'," + str(
#             userconfidence) + ");"
#         common.connectmysql(x)
#     except:
#         dispatcher.utter_message(text='save_error')
#     return []

class mood_or_unhappy(Action):
    def name(self) -> Text:
        return "action_mood_or_unhappy"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        userid = tracker.current_state()['sender_id']
        conn = common.POOL.connection(shareable=False)
        cursor = conn.cursor()
        try:
            cursor.execute(
                "Select * from `user_info` where `id` = '" + userid + "';")
            x = cursor.fetchone()
            nickname = x[1]
            names = x[2]
            if nickname is None:
                nickname = ''
            if names is None:
                names = ''
            sql = "insert into `user_result`(`id`,`user_info_id`,`topic_type`,`name`,`nick_name`,`test_batch`) values('" + common.getuid() + "','" + userid + "',2,'" + \
                  names + "','" + nickname + "',"+str(x[35])+");"
            common.connectmysql(sql)
        except:
            dispatcher.utter_message('机器人错误 错误码0x103')
        finally:
            cursor.close()
            conn.close()
        dispatcher.utter_message(text="最近感觉情绪怎么样呀？有没有觉得自己不开心呀？", buttons=[
            {"payload": "/mood_or_unhappy_no", "title": "大部分时间都挺好的呀", "life": 1},
            {"payload": "/mood_or_unhappy_yes", "title": "大部分时间都不太好", "life": 1}
        ], mjson_message={"module_type": 4})

        return []


class action_angry_impulsive(Action):
    def name(self) -> Text:
        return "action_angry_impulsive"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="你最近有没有感觉到自己容易生气、容易冲动，或者精力旺盛、思维跳跃快速？", buttons=[
            {"payload": "/angry_impulsive_yes", "title": "好像有一点", "life": 1},
            {"payload": "/into_chat", "title": "没觉得呀", "life": 1},
        ])
        return []


class health_chat(Action):
    def name(self) -> Text:
        return "action_health_chat"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="你的情绪维持在很好的状态哦！很棒很棒！下面我们来聊聊天吧~", buttons=[
            {"payload": "/into_chat", "title": "好呀好呀~", "life": 1},
        ])
        return []


class action_mood_down_issue(Action):
    def name(self) -> Text:
        return "action_mood_down_issue"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="最近有什么使你情绪低落的事情发生吗？", buttons=[
            {"payload": "/mood_down_issue_yes", "title": "有的，很烦", "life": 1},
            {"payload": "/mood_down_issue_no", "title": "没有啥具体的事情发生", "life": 1},
        ])
        return []


class action_reasonless_low_mood(Action):
    def name(self) -> Text:
        return "action_reasonless_low_mood"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="之前出现过没有任何理由的情绪低落情况吗？", buttons=[
            {"payload": "/reasonless_low_mood_yes", "title": "有哎", "life": 1},
            {"payload": "/reasonless_low_mood_no", "title": "没有，我的情绪低落都是有原因的", "life": 1},
        ])
        return []


class action_day_heavy(Action):
    def name(self) -> Text:
        return "action_day_heavy"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="情绪低落的情况会随着昼夜的交替出现昼重夜轻的规律吗？", buttons=[
            {"payload": "/day_heavy_yes", "title": "好像是这样，早上的时候会更严重一些", "life": 1},
            {"payload": "/day_heavy_no", "title": "没觉得", "life": 1},
        ])
        return []


class action_environmental_impact(Action):
    def name(self) -> Text:
        return "action_environmental_impact"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="你的情绪波动性很大，并且情绪与现实生活中发生的事情联系密切，容易受到身边环境的影响吗？", buttons=[
            {"payload": "/environmental_impact_yes", "title": "没错！我的情绪波动有些大，而且身边发生的事情很容易影响我的心情", "life": 1},
            {"payload": "/environmental_impact_no", "title": "没呀，我的情绪比较稳定，身边发生的事情对我的影响不大", "life": 1},
        ])
        return []


class action_pain_trouble(Action):
    def name(self) -> Text:
        return "action_pain_trouble"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="情绪低落的情况让你感到十分的痛苦和烦恼，甚至对于日常的人际交往、学习、生活等都产生了影响？", buttons=[
            {"payload": "/pain_trouble_yes", "title": "是的，而且我不想再这样下去了，我希望每天都开开心心的！", "life": 1},
            {"payload": "/pain_trouble_no", "title": "没有，我虽然感到低落，但是并没有因此感到烦恼，也没有影响到我的生活", "life": 1},
        ])
        return []


class action_mood_down_issue_when(Action):
    def name(self) -> Text:
        return "action_mood_down_issue_when"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="情绪低落这个情况从什么时候开始的呀🧐？", buttons=[
            {"payload": "/mood_down_issue_when_before18", "title": "18岁之前", "life": 1},
            {"payload": "/mood_down_issue_when_after18", "title": "18岁以后", "life": 1},
        ])
        return []


class action_initially_irritated(Action):
    def name(self) -> Text:
        return "action_initially_irritated"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是不是在最初的时候仅仅是容易被他人激怒，而产生口头或行为上的脾气爆发，现在变得频繁了，每周至少会有三次情绪的爆发？？", buttons=[
            {"payload": "/initially_irritated_yes", "title": "就是这样！很容易情绪就会爆发，我也不想这样的", "life": 1},
            {"payload": "/initially_irritated_no", "title": "不是的，跟我的情况不一样", "life": 1},
        ])
        return []


class drug_or_disease(Action):
    def name(self) -> Text:
        return "action_drug_or_disease"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        # 题-1
        dispatcher.utter_message(text='最近有服用一些药物吗？有一些躯体及脑部的疾病或者外伤吗？')
        dispatcher.utter_message(text='不是指普通的治疗感冒或发烧的药物哦')
        dispatcher.utter_message(
            text='而是指病因学上的一些物质致幻剂、吸入剂、鸦片类、苯环己哌啶、镇静剂、催眠剂、抗焦虑剂。处方药物包括抗高血压药、口服避孕药、皮质类固醇、代谢类固醇、抗癌剂、镇痛剂、抗胆碱剂、心脏用药。')
        dispatcher.utter_message(text='疾病也是指一些特定的疾病哦')
        dispatcher.utter_message(
            text='病因学上的一般躯体疾病包括：神经系统退行性疾病（如：帕金森病)，脑血管疾病(如：中风)，代谢性疾病(如：维生素B12缺乏)，内分泌疾病(如：甲状腺功能亢进或减退﹑肾上腺皮质功能亢进或减退)，病毒或其它感染(如：肝炎、传染性单核细胞增多症、艾滋病)和某些癌症(如：胰腺癌)。')
        # dispatcher.utter_message(text='不是指普通的治疗感冒或发烧的药物哦')
        dispatcher.utter_message(text="怎么样？你有服用上述的药物或者有上面说的这些疾病吗？", buttons=[
            {"payload": "/drug_or_disease_yes", "title": "有", "life": 1},
            {"payload": "/drug_or_disease_no", "title": "我仔细看了一遍，并没有啊", "life": 1},
        ])
        return []


class action_emotional_burst(Action):
    def name(self) -> Text:
        return "action_emotional_burst"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="你是否觉得自己持续地易产生剧烈的情感反应？即使遇到轻微不愉快的情况，也会有情绪爆发。在情绪不爆发的时间里，也总是会出现一些过度反应的情况？", buttons=[
            {"payload": "/emotional_burst_yes", "title": "嗯嗯，经常有这种情况发生", "life": 1},
            {"payload": "/emotional_burst_no", "title": "我没有出现这样的情况哦", "life": 1},
        ])
        return []


class action_mood_down_2year(Action):
    def name(self) -> Text:
        return "action_mood_down_2year"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="请你认真地思考一下，情绪低落的情况是否超过两年？", buttons=[
            {"payload": "/mood_down_2year_yes", "title": "是的", "life": 1},
            {"payload": "/mood_down_2year_no", "title": "没有", "life": 1},
        ])
        return []


##----------------------药物和疾病的询问----------------------------------
class long_term_illness(Action):
    def name(self) -> Text:
        return "action_long_term_illness"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        # 题-1
        dispatcher.utter_message(text='现在或曾经有没有患过一些长期疾病？')
        dispatcher.utter_message(text='具体包括下面这些：代谢疾病（如：维生素B12缺乏）、内分泌疾病(如：甲状腺类疾病)、病毒或者其他感染（如：梅毒、肝炎、艾滋病等）或者某些癌症？')
        dispatcher.utter_message(text="怎么样？你有满足上面所说情况的一种吗？", buttons=[
            {"payload": "/long_term_illness_yes", "title": "有😢"},
            {"payload": "/long_term_illness_no", "title": "我仔细看了一遍，都没有啊"},
        ])
        if tracker.current_state()['events'][-2]['text'] == '/angry_impulsive_yes':
            return [SlotSet("into_illness", "躁狂")]
        elif tracker.current_state()['events'][-2]['text'] == '/mood_down_2year_no':
            return [SlotSet("into_illness", "重性")]
        else:
            return [SlotSet("into_illness", "持续性")]


class illness_change(Action):
    def name(self) -> Text:
        return "action_illness_change"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        # 题-1
        dispatcher.utter_message(text="无理由情绪低落的情况在发病时或病情加重时产生，随着病情缓解而缓解吗？", buttons=[
            {"payload": "/illness_change_yes", "title": "嗯嗯，跟我的情况一样", "life": 1},
            {"payload": "/illness_change_no", "title": "不是的", "life": 1},
        ])
        return []


class alcoholism(Action):
    def name(self) -> Text:
        return "action_alcoholism"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        # 题-1量
        tmp = tracker.current_state()
        dispatcher.utter_message(text='是否有酗酒、赌博、吸毒、大抽烟等行为？')
        dispatcher.utter_message(text="并且这些行为的开始、结束或者使用剂量过多而导致的中毒使你产生了情绪低落的情况？", buttons=[
            {"payload": "/alcoholism_yes", "title": "是的", "life": 1},
            {"payload": "/alcoholism_no", "title": "不是的", "life": 1},
        ])
        return []


class stop_medicine(Action):
    def name(self) -> Text:
        return "action_stop_medicine"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        # 题-1量
        tmp = tracker.current_state()
        dispatcher.utter_message(text='最近是否有某些药物的停药情况？')
        dispatcher.utter_message(text="比如高血压药、口服避孕药、皮质类固醇、代谢类固醇、抗癌剂、镇痛剂、抗胆碱剂、心脏用药", buttons=[
            {"payload": "/stop_medicine_yes", "title": "嗯嗯，有的", "life": 1},
            {"payload": "/stop_medicine_no", "title": "没有", "life": 1},
        ])
        return []


class substance_poison(Action):
    def name(self) -> Text:
        return "action_substance_poison"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        # 题-1量
        dispatcher.utter_message(text="是否存在一些物质慢性中毒？如重金属慢性中毒等", buttons=[
            {"payload": "/substance_poison_yes", "title": "有", "life": 1},
            {"payload": "/substance_poison_no", "title": "没有", "life": 1},
        ])
        return []


class substance_change(Action):
    def name(self) -> Text:
        return "action_substance_change"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        # 题-1量
        dispatcher.utter_message(text='无理由情绪低落的情况是在接触物质时或不久后产生吗？')
        dispatcher.utter_message(text="戒断或中毒停止后无理由情绪低落的情况还维持了相当长一段时间吗", buttons=[
            {"payload": "/substance_change_yes", "title": "嗯嗯，是的", "life": 1},
            {"payload": "/substance_change_no", "title": "不是", "life": 1},
        ])
        return []


class allergies_or_dullness_1(Action):
    def name(self) -> Text:
        return "action_allergies_or_dullness_1"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        heart_end.midterm_encourage(dispatcher, tracker)
        dispatcher.utter_message(text="你是否出现过对某些东西特别敏感，或者特别迟钝的情况呢？", buttons=[
            {"payload": "/allergies_or_dullness_1_yes", "title": "好像有", "life": 1},
            {"payload": "/allergies_or_dullness_1_no", "title": "哎？没有过", "life": 1},
        ])
        return []


class distort_2(Action):
    def name(self) -> Text:
        return "action_distort_2"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否出现过把一些正常的东西看得扭曲的现象？", buttons=[
            {"payload": "/distort_2_yes", "title": "有过！", "life": 1},
            {"payload": "/distort_2_no", "title": "没有", "life": 1},
        ])
        return []


class vision_3(Action):
    def name(self) -> Text:
        return "action_vision_3"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="有没有听见过别人听不到的声音、看见过别人看不到的东西？", buttons=[
            {"payload": "/vision_3_yes", "title": "有过这种经历😲", "life": 1},
            {"payload": "/vision_3_no", "title": "没有", "life": 1},
        ])
        return []


class strange_4(Action):
    def name(self) -> Text:
        return "action_strange_4"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否感受到过一些本不应该出现的或者奇妙的感觉？", buttons=[
            {"payload": "/strange_4_yes", "title": "有过！", "life": 1},
            {"payload": "/strange_4_no", "title": "没有", "life": 1},
        ])
        return []


class stupid_5(Action):
    def name(self) -> Text:
        return "action_stupid_5"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否有时觉得自己脑子非常不好使，比如做简单加减乘除法对自己来说都有点困难？", buttons=[
            {"payload": "/stupid_5_yes", "title": "有过！", "life": 1},
            {"payload": "/stupid_5_no", "title": "没有", "life": 1},
        ])
        return []


class unspeakable_6(Action):
    def name(self) -> Text:
        return "action_unspeakable_6"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text='有没有出现过说话或者行动不利索的情况？')
        dispatcher.utter_message(text="比如基本听不懂别人说话，或者简单的日常生活都有点难以自理？", buttons=[
            {"payload": "/unspeakable_6_yes", "title": "有过！", "life": 1},
            {"payload": "/unspeakable_6_no", "title": "没有", "life": 1},
        ])
        return []


class deep_memory_7(Action):
    def name(self) -> Text:
        return "action_deep_memory_7"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否有某些记忆过于深刻？", buttons=[
            {"payload": "/deep_memory_7_yes", "title": "有过！", "life": 1},
            {"payload": "/deep_memory_7_no", "title": "没有", "life": 1},
        ])
        return []


class lost_memory_8(Action):
    def name(self) -> Text:
        return "action_lost_memory_8"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否觉得自己失去了某些记忆？", buttons=[
            {"payload": "/lost_memory_8_yes", "title": "是的", "life": 1},
            {"payload": "/lost_memory_8_no", "title": "没有", "life": 1},
        ])
        return []


class inconsistent_memory_9(Action):
    def name(self) -> Text:
        return "action_inconsistent_memory_9"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="有没有觉得自己的脑海中存在一些与真实发生的事情不符的记忆？", buttons=[
            {"payload": "/inconsistent_memory_9_yes", "title": "有有有", "life": 1},
            {"payload": "/inconsistent_memory_9_no", "title": "没觉得", "life": 1},
        ])
        return []


class harm_you_10(Action):
    def name(self) -> Text:
        return "action_harm_you_10"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否总觉得别人要害自己、甚至想杀自己？", buttons=[
            {"payload": "/harm_you_10_yes", "title": "对，有人想害我", "life": 1},
            {"payload": "/harm_you_10_no", "title": "没有", "life": 1},
        ])
        return []


class feel_cheating_11(Action):
    def name(self) -> Text:
        return "action_feel_cheating_11"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否觉得自己的男/女朋友喜欢上别的人了？", buttons=[
            {"payload": "/feel_cheating_11_yes", "title": "有怀疑", "life": 1},
            {"payload": "/feel_cheating_11_no", "title": "没有", "life": 1},
        ])
        return []


class feel_rich_12(Action):
    def name(self) -> Text:
        return "action_feel_rich_12"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否觉得自己很富有或者很有名？", buttons=[
            {"payload": "/feel_rich_12_yes", "title": "对呀，我就是很厉害啊", "life": 1},
            {"payload": "/feel_rich_12_no", "title": "没觉得", "life": 1},
        ])
        return []


class self_blame_13(Action):
    def name(self) -> Text:
        return "action_self_blame_13"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否觉得自己哪都有问题、啥事都做不好、事情做不好都怪自己的情况？", buttons=[
            {"payload": "/self_blame_13_yes", "title": "有点", "life": 1},
            {"payload": "/self_blame_13_no", "title": "没有", "life": 1},
        ])
        return []


class like_you_14(Action):
    def name(self) -> Text:
        return "action_like_you_14"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否坚信某个不会喜欢自己的人就是喜欢自己？", buttons=[
            {"payload": "/like_you_14_yes", "title": "有的", "life": 1},
            {"payload": "/like_you_14_no", "title": "没有", "life": 1},
        ])
        return []


class control_you_15(Action):
    def name(self) -> Text:
        return "action_control_you_15"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否感觉自己的思想行为被别人，或者被一些电磁波等手段和方法所控制？", buttons=[
            {"payload": "/control_you_15_yes", "title": "有的", "life": 1},
            {"payload": "/control_you_15_no", "title": "没有", "life": 1},
        ])
        return []


class somebody_in_16(Action):
    def name(self) -> Text:
        return "action_somebody_in_16"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否觉得自己居住的家里、宿舍进了其他的人，让你感到不安全？", buttons=[
            {"payload": "/somebody_in_16_yes", "title": "有的", "life": 1},
            {"payload": "/somebody_in_16_no", "title": "没有", "life": 1},
        ])
        return []


class punish_you_17(Action):
    def name(self) -> Text:
        return "action_punish_you_17"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否觉得自己有罪、做错事了应该受惩罚的坚定想法？", buttons=[
            {"payload": "/punish_you_17_yes", "title": "有的", "life": 1},
            {"payload": "/punish_you_17_no", "title": "没有", "life": 1},
        ])
        return []


class get_your_money_18(Action):
    def name(self) -> Text:
        return "action_get_your_money_18"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="是否有人想尽方法获取你的钱财？", buttons=[
            {"payload": "/get_your_money_18_yes", "title": "有的", "life": 1},
            {"payload": "/get_your_money_18_no", "title": "没有", "life": 1},
        ])
        return []


# --------------三个标准的第一步写到一起--------------------
class mania_major_persistent_criteria_1(Action):
    def name(self) -> Text:
        return "action_mania_major_persistent_criteria_1"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        # 题-1
        rid = tracker.current_state()['sender_id']
        common.connectredis(1, rid, 0)
        uptemp = tracker.get_slot('into_illness')
        if tracker.current_state()['events'][-2]['text'] == '/no_reason_excited_yes':
            uptemp = "躁狂"
        if tracker.current_state()['events'][-2]['text'] == '/low_mood_persists_no':
            uptemp = "重性"
        if uptemp == "躁狂":
            dispatcher.utter_message(text='你有没有在连续一周的时间内，几乎每一天的大部分时间都感到明显异常的、持续性的高涨（持续的过度兴奋）?')
            dispatcher.utter_message(text="或容易被他人激怒，或异常的持续性的活动增多或精力旺盛？", buttons=[
                {"payload": "/mania_criteria_1_yes", "title": "我有这种情况", "life": 1},
                {"payload": "/mania_criteria_1_no", "title": "没有", "life": 1},
            ])
            return [SlotSet("into_illness", "躁狂")]
        elif uptemp == "重性":
            dispatcher.utter_message(text="几乎大部分时间都感觉自己不开心，感到空虚、无望、悲伤吗？", buttons=[
                {"payload": "/major_depression_criteria_1_yes", "title": "我有这种情况"},
                {"payload": "/major_depression_criteria_1_no", "title": "没有"},
            ])
            return [SlotSet("into_illness", "重性")]
        elif uptemp == "持续性":
            dispatcher.utter_message(text='你的一天中大部分时间都处于抑郁状态，至少有2年之久吗？ 这可以根据主观描述或他人观察得出。')
            dispatcher.utter_message(text="如果你是未成年人，这可能表现为易怒😡，持续时间至少1年吗？", buttons=[
                {"payload": "/persistent_depression_criteria_1_yes", "title": "是的，甚至更久", "life": 1},
                {"payload": "/persistent_depression_criteria_1_no", "title": "没有", "life": 1},
            ])
            return [SlotSet("into_illness", "持续性")]
        else:
            dispatcher.utter_message(text='对不起，小E好笨呀，没明白你的意思……')
            dispatcher.utter_message(text="再给小E个机会好吗？~", buttons=[
                {"payload": "/mood_or_unhappy_yes", "title": "好呀，只给你一次机会呦"},
            ])
            return []


# -----------------------躁狂第二部分-----------------------
class mania_criteria_2a(Action):
    def name(self) -> Text:
        return "action_mania_criteria_2a"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text='你在情绪低落、精力旺盛或活动增加时，是否存在以下症状:')
        dispatcher.utter_message(text="a.自尊心膨胀或夸大", buttons=[
            {"payload": "/mania_criteria_2a_yes", "title": "好像有点儿"},
            {"payload": "/mania_criteria_2a_no", "title": "没有"},
        ])
        return [SlotSet("is_female", None)]


class mania_criteria_2b(Action):
    def name(self) -> Text:
        return "action_mania_criteria_2b"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'mania_criteria_2a_yes':
            rid = tracker.current_state()['sender_id']
            temp = common.connectredis(0, rid, 0) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="b．睡眠的需求减少（例如,仅仅睡了3小时,就感到足够了） ", buttons=[
            {"payload": "/mania_criteria_2b_yes", "title": "对对！"},
            {"payload": "/mania_criteria_2b_no", "title": "没有"},
        ])
        return []


class mania_criteria_2c(Action):
    def name(self) -> Text:
        return "action_mania_criteria_2c"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'mania_criteria_2b_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="c、健谈或有持续讲话的压力感。 ", buttons=[
            {"payload": "/mania_criteria_2c_yes", "title": "对"},
            {"payload": "/mania_criteria_2c_no", "title": "没有"},
        ])
        return []


class mania_criteria_2d(Action):
    def name(self) -> Text:
        return "action_mania_criteria_2d"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'mania_criteria_2c_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="d．意念飘忽，自己所想的事情一件接一件，快速转换地思考一些原本不是非常连贯的话题，话比平时明显增多，但是一般人不能听懂其中逻辑。 ", buttons=[
            {"payload": "/mania_criteria_2d_yes", "title": "对"},
            {"payload": "/mania_criteria_2d_no", "title": "没有"},
        ])
        return []


class mania_criteria_2e(Action):
    def name(self) -> Text:
        return "action_mania_criteria_2e"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'mania_criteria_2d_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="e.自我报告或被观察到的随境转移(即，注意力太容易被不重要或无关的外界刺激吸引）。", buttons=[
            {"payload": "/mania_criteria_2e_yes", "title": "对"},
            {"payload": "/mania_criteria_2e_no", "title": "没有"},
        ])
        return []


class mania_criteria_2f(Action):
    def name(self) -> Text:
        return "action_mania_criteria_2f"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'mania_criteria_2e_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text='f.有目标的活动增多（工作或上学时的社交，或性活动）。')
        dispatcher.utter_message(text="或精神运动性激越（即,思考的内容难以专注在一个主题上，行为表现为坐不住、站不住、想到处溜达、感觉自己非常烦躁）", buttons=[
            {"payload": "/mania_criteria_2f_yes", "title": "对"},
            {"payload": "/mania_criteria_2f_no", "title": "没有"},
        ])
        return []


class mania_criteria_2g(Action):
    def name(self) -> Text:
        return "action_mania_criteria_2g"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'mania_criteria_2f_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="g.过度地参与那些结果痛苦的可能性高的活动（如无节制购物，轻率性行为，愚蠢商业投资)", buttons=[
            {"payload": "/mania_criteria_2g_yes", "title": "对"},
            {"payload": "/mania_criteria_2g_no", "title": "没有"},
        ])
        return []


class compute_mania_criteria_ag_count(Action):
    def name(self) -> Text:
        return "action_compute_mania_criteria_ag_count"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        rid = tracker.current_state()['sender_id']
        temp = int(common.connectredis(0, rid, 0))
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'mania_criteria_2g_yes':
            temp = temp + 1
        if temp > 3:
            return [SlotSet("mania_criteria_ag_count", True)]
        else:
            return [SlotSet("mania_criteria_ag_count", False)]


# ---------------------重度----------------------------
class major_depression_criteria_2(Action):
    def name(self) -> Text:
        return "action_major_depression_criteria_2"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        rid = tracker.current_state()['sender_id']
        common.connectredis(1, rid, 0)
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'major_depression_criteria_1_yes':
            # temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, 1)
        dispatcher.utter_message(text="感觉到自己的兴趣明显减少吗？", buttons=[
            {"payload": "/major_depression_criteria_2_yes", "title": "有哎"},
            {"payload": "/major_depression_criteria_2_no", "title": "没有啊，还是会对很多东西感兴趣"},
        ])
        return []


# class compute_major_depression_criteria_count1(Action):
#     def name(self) -> Text:
#         return "action_compute_major_depression_criteria_count1"
#     async def run(
#             self,
#             dispatcher: CollectingDispatcher,
#             tracker: Tracker,
#             domain: Dict[Text, Any],
#     ) -> List[Dict[Text, Any]]:
#         rid = tracker.current_state()['sender_id']
#         temp = int(common.connectredis(0, rid, 0))
#         if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'major_depression_criteria_2_yes':
#             temp=temp+1
#             common.connectredis(1, rid, temp)
#         dispatcher.utter_message(text="compute处重度个数" + str(temp))
#         if int(temp)<=0:
#             dispatcher.utter_message(text="进入填槽major_depression_criteria_count1  @@false" )
#             return [SlotSet("major_depression_criteria_count1", False)]
#         # return []
#         else:
#             dispatcher.utter_message(text="进入填槽major_depression_criteria_count1  @@true")
#             return [SlotSet("major_depression_criteria_count1", True)]

class major_depression_criteria_3(Action):
    def name(self) -> Text:
        return "action_major_depression_criteria_3"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        a = []
        rid = tracker.current_state()['sender_id']
        temp = int(common.connectredis(0, rid, 0))
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'major_depression_criteria_2_yes':
            temp = temp + 1
            common.connectredis(1, rid, temp)
        if temp == 0:
            # a.append(FollowupAction('action_mood_swings'))
            # return a

            # text = "情绪波动"
            # a=[]
            # try:
            #     # y = "update `user_result` set level_of_depression_psq9=" + text + ",is_end=1 where `user_info_id` ='" + rid + "'and is_end=0 and topic_type= 2;"
            #     # common.connectmysql(y)
            #     a=heart_end.save_end(dispatcher, tracker, text)
            #     dispatcher.utter_message(text="谢谢你跟我聊天，我对你有了更深的了解啦~")
            # except:
            #     print("major_depression_criteria_3 l:1261 save error")
            # return a
            text = "情绪波动"
            a = heart_end.save_end(dispatcher, tracker, text)
            dispatcher.utter_message(text="呜呼……所有的问题都问完啦~")
            dispatcher.utter_message(text="你想要看看最终的诊断结果吗？", buttons=[
                {"payload": "/result_yes", "title": "想看想看！"},
                {"payload": "/result_no", "title": "不要了吧"},
            ])
            return a
        else:
            dispatcher.utter_message(text="你的食欲有没有发生变化？变得吃很多或者吃很少？？")
            dispatcher.utter_message(text="体重是否在无意的情况下发生变化？体重增加或者减少原体重的5%？", buttons=[
                {"payload": "/major_depression_criteria_3_yes", "title": "有哎"},
                {"payload": "/major_depression_criteria_3_no", "title": "没有变化"},
            ])
            return [SlotSet("is_female", None)]


class major_depression_criteria_4(Action):
    def name(self) -> Text:
        return "action_major_depression_criteria_4"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'major_depression_criteria_3_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="你的睡眠状况好吗？")
        dispatcher.utter_message(text="有出现早醒、难入睡、睡眠过程中质量不好或者睡眠过多等情况吗？", buttons=[
            {"payload": "/major_depression_criteria_4_yes", "title": "嗯嗯！"},
            {"payload": "/major_depression_criteria_4_no", "title": "没有，我的睡眠质量还是挺好的"},
        ])
        return []


class major_depression_criteria_5(Action):
    def name(self) -> Text:
        return "action_major_depression_criteria_5"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'major_depression_criteria_4_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="感觉自己有点迟钝吗？", buttons=[
            {"payload": "/major_depression_criteria_5_yes", "title": "有有有，最近尤其明显"},
            {"payload": "/major_depression_criteria_5_no", "title": "没有"},
        ])
        return []


class major_depression_criteria_6(Action):
    def name(self) -> Text:
        return "action_major_depression_criteria_6"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'major_depression_criteria_5_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="容易感到疲惫或精力不足吗？", buttons=[
            {"payload": "/major_depression_criteria_6_yes", "title": "对"},
            {"payload": "/major_depression_criteria_6_no", "title": "没有啊，一直感到精力充沛"},
        ])
        return []


class major_depression_criteria_7(Action):
    def name(self) -> Text:
        return "action_major_depression_criteria_7"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'major_depression_criteria_6_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="几乎每天都感觉自己没有价值或者自责吗？", buttons=[
            {"payload": "/major_depression_criteria_7_yes", "title": "会有一点"},
            {"payload": "/major_depression_criteria_7_no", "title": "不会"},
        ])
        return []


class major_depression_criteria_8(Action):
    def name(self) -> Text:
        return "action_major_depression_criteria_8"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'major_depression_criteria_7_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="感觉到自己思考速度比较慢，注意力不集中，记不住东西吗？", buttons=[
            {"payload": "/major_depression_criteria_8_yes", "title": "有点……"},
            {"payload": "/major_depression_criteria_8_no", "title": "没有"},
        ])
        return []


class major_depression_criteria_9(Action):
    def name(self) -> Text:
        return "action_major_depression_criteria_9"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'major_depression_criteria_8_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="反复想到自杀的想法，或者有自杀的具体计划吗？（诸如跳楼还是割腕）", buttons=[
            {"payload": "/major_depression_criteria_9_yes", "title": "有的，会在脑海中闪过自杀的想法"},
            {"payload": "/major_depression_criteria_9_no", "title": "没有"},
        ])
        return []


# --------------------持续性------------------------
class persistent_depression_criteria_2a(Action):
    def name(self) -> Text:
        return "action_persistent_depression_criteria_2a"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        rid = tracker.current_state()['sender_id']
        common.connectredis(1, rid, 0)
        dispatcher.utter_message(text='当你的情绪低落时，是否存在以下情况呢:')
        dispatcher.utter_message(text="a.食欲不振或暴饮暴食。", buttons=[
            {"payload": "/persistent_depression_criteria_2a_yes", "title": "有哎"},
            {"payload": "/persistent_depression_criteria_2a_no", "title": "没有"},
        ])
        return [SlotSet("is_female", None)]


class persistent_depression_criteria_2b(Action):
    def name(self) -> Text:
        return "action_persistent_depression_criteria_2b"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent'][
            'name'] == 'persistent_depression_criteria_2a_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="b.失眠或嗜睡", buttons=[
            {"payload": "/persistent_depression_criteria_2b_yes", "title": "有点儿"},
            {"payload": "/persistent_depression_criteria_2b_no", "title": "没有"},
        ])
        return []


class persistent_depression_criteria_2c(Action):
    def name(self) -> Text:
        return "action_persistent_depression_criteria_2c"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent'][
            'name'] == 'persistent_depression_criteria_2b_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="c.精力不足或疲劳。", buttons=[
            {"payload": "/persistent_depression_criteria_2c_yes", "title": "有点儿"},
            {"payload": "/persistent_depression_criteria_2c_no", "title": "没有"},
        ])
        return []


class persistent_depression_criteria_2d(Action):
    def name(self) -> Text:
        return "action_persistent_depression_criteria_2d"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent'][
            'name'] == 'persistent_depression_criteria_2c_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="d.低自尊。", buttons=[
            {"payload": "/persistent_depression_criteria_2d_yes", "title": "有点儿"},
            {"payload": "/persistent_depression_criteria_2d_no", "title": "没有"},
        ])
        return []


class persistent_depression_criteria_2e(Action):
    def name(self) -> Text:
        return "action_persistent_depression_criteria_2e"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent'][
            'name'] == 'persistent_depression_criteria_2d_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="e.注意力不集中或难以做出决定。", buttons=[
            {"payload": "/persistent_depression_criteria_2e_yes", "title": "有点儿"},
            {"payload": "/persistent_depression_criteria_2e_no", "title": "没有"},
        ])
        return []


class persistent_depression_criteria_2f(Action):
    def name(self) -> Text:
        return "action_persistent_depression_criteria_2f"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        if tracker.current_state()['events'][-2]['parse_data']['intent'][
            'name'] == 'persistent_depression_criteria_2e_yes':
            rid = tracker.current_state()['sender_id']
            temp = int(common.connectredis(0, rid, 0)) + 1
            common.connectredis(1, rid, temp)
        dispatcher.utter_message(text="f.绝望的感觉。", buttons=[
            {"payload": "/persistent_depression_criteria_2f_yes", "title": "有点儿"},
            {"payload": "/persistent_depression_criteria_2f_no", "title": "没有"},
        ])
        return []


class compute_persistent_depression_criteria_af_count(Action):
    def name(self) -> Text:
        return "action_compute_persistent_depression_criteria_af_count"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        rid = tracker.current_state()['sender_id']
        temp = int(common.connectredis(0, rid, 0))
        if tracker.current_state()['events'][-2]['parse_data']['intent'][
            'name'] == 'persistent_depression_criteria_2f_yes':
            temp = temp + 1
            common.connectredis(1, rid, temp)
        if temp > 2:
            return [SlotSet("persistent_depression_criteria_af_count", True)]
        else:
            return [SlotSet("persistent_depression_criteria_af_count", False)]


# ----------------------------------------------------------------------------------------------------------------#


class action_low_mood_persists(Action):
    def name(self) -> Text:
        return "action_low_mood_persists"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="在近2年时间里(未成年1年)，上述两个情况症状从来没有一次消失超过2个月。即低情绪状况一直存在，没有一段时间情绪更好或者更加的不好。", buttons=[
            {"payload": "/low_mood_persists_yes", "title": "好像是这样", "life": 1},
            {"payload": "/low_mood_persists_no", "title": "不是的", "life": 1},
        ])
        return []


class action_no_reason_excited(Action):
    def name(self) -> Text:
        return "action_no_reason_excited"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        a = []
        rid = tracker.current_state()['sender_id']
        temp = int(common.connectredis(0, rid, 0))
        if tracker.current_state()['events'][-2]['parse_data']['intent']['name'] == 'major_depression_criteria_9_yes':
            temp = temp + 1
            common.connectredis(1, rid, temp)
        if temp <= 5 and tracker.get_slot('into_illness') == "重性":
            # a.append(FollowupAction('action_mood_swings'))
            text = "情绪波动"
            a = heart_end.save_end(dispatcher, tracker, text)
            dispatcher.utter_message(text="呜呼……所有的问题都问完啦~")
            dispatcher.utter_message(text="你想要看看最终的诊断结果吗？", buttons=[
                {"payload": "/result_yes", "title": "想看想看！", "life": 1},
                {"payload": "/result_no", "title": "不要了吧", "life": 1},
            ])
            return a
        else:
            dispatcher.utter_message(text="是否有一段时间感觉自己出现容易生气、容易冲动、精力旺盛、思维跳跃快速、莫明兴奋、情绪忽高忽低等情况？", buttons=[
                {"payload": "/no_reason_excited_yes", "title": "嗯嗯！有的", "life": 1},
                {"payload": "/no_reason_excited_no", "title": "没有哦", "life": 1},
            ])
            return []


class action_mood_with_seasons(Action):
    def name(self) -> Text:
        return "action_mood_with_seasons"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        heart_end.midterm_encourage(dispatcher, tracker)
        dispatcher.utter_message(text="你的情绪变化是否随着季节变化而有规律地发生变化呢??", buttons=[
            {"payload": "/mood_with_seasons_yes", "title": "嗯嗯！有的", "life": 1},
            {"payload": "/mood_with_seasons_no", "title": "没有哦"},
        ])
        userid = tracker.current_state()['sender_id']
        conn = common.POOL.connection(shareable=False)
        cursor = conn.cursor()
        try:
            cursor.execute("Select * from `user_info` where `id` = '" + userid + "';")
            x = cursor.fetchone()
            # print("\n x="+x)
            a = []
            if x[3] != 1:
                a.append(SlotSet("is_female", False))
            else:
                a.append(SlotSet("is_female", True))
        except Exception as e:
            pass
        finally:
            cursor.close()
            conn.close()
        return a


class action_period(Action):
    def name(self) -> Text:
        return "action_period"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        a = []
        temp = tracker.get_slot('is_female')
        if temp == False:
            a.append(FollowupAction('action_study_life_impact'))
            return a
        else:
            dispatcher.utter_message(text="你有没有觉得你的情绪变化受到经期的影响呀？", buttons=[
                {"payload": "/period_yes", "title": "有哎", "life": 1},
                {"payload": "/period_no", "title": "没觉得"},
            ])
        return []


class period_mood_change(Action):
    def name(self) -> Text:
        return "action_period_mood_change"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="在一年内的大多数月经周期中，抑郁症状有没有呈现严格的周期性变化？")
        dispatcher.utter_message(text="抑郁症状必须在月经来潮前一周出现，在月经来潮后的几天内开始改善，在月经来潮后的一周内减少或消失？", buttons=[
            {"payload": "/period_mood_change_yes", "title": "跟我情况一样！", "life": 1},
            {"payload": "/period_mood_change_no", "title": "没有这种规律", "life": 1},
        ])
        return []


class action_study_life_impact(Action):
    def name(self) -> Text:
        return "action_study_life_impact"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="上述所有情况对你的学习和生活造成了多大的影响？可以用表示程度的词语简单表述一下吗？")
        return []


# class action_study_life_impact_content(Action):
#     def name(self) -> Text:
#         return "action_study_life_impact_content"
#     async def run(
#             self,
#             dispatcher: CollectingDispatcher,
#             tracker: Tracker,
#             domain: Dict[Text, Any],
#     ) -> List[Dict[Text, Any]]:
#         dispatcher.utter_message(text="上述所有情况对你的学习和生活造成了多大的影响？可以用表示程度的语言简单表述一下吗？")
#         return []
class action_suicide_thought(Action):
    def name(self) -> Text:
        return "action_suicide_thought"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        userid = tracker.current_state()['sender_id']
        conn = common.POOL.connection(shareable=False)
        cursor = conn.cursor()
        c = tracker.get_slot('study_life_impact_content')
        try:
            if c != None:
                # sql = "update `user_result` set topic_num=241,`user_scenes_intent`='/scence_one' where `user_info_id` ='" + userid + "' and `is_end` = 0 and `topic_type` = 0;"
                sql = "update `user_result` set `study_life_impact_content`= '" + str(
                    c) + "'  where `user_info_id` ='" + userid + "'and `is_end` = 0 and topic_type= 2 order by create_time desc limit 1;"
                common.connectmysql(sql)
        except:
            dispatcher.utter_message(text=" ！！保存学习生活的影响不成功 ")
        finally:
            cursor.close()
            conn.close()
        dispatcher.utter_message(text="谢谢你向我分享你学习和生活中的事情😊😊😊~~")
        dispatcher.utter_message(text="接下来你可以偷偷告诉我，你有过关于自杀的想法吗？", buttons=[
            {"payload": "/suicide_thought_yes", "title": "有偷偷想过……", "life": 1},
            {"payload": "/suicide_thought_no", "title": "没有过", "life": 1},
        ])
        return []


class action_suicide_thought_how_long(Action):
    def name(self) -> Text:
        return "action_suicide_thought_how_long"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="有这种想法多久了呢？", buttons=[
            {"payload": "/suicide_1week_2week", "title": "一到两周"},
            {"payload": "/suicide_2week_1month", "title": "两周到一个月"},
            {"payload": "/suicide_1month_2month", "title": "一个月到两个月"},
            {"payload": "/suicide_2month_6month", "title": "两个月到六个月"},
            {"payload": "/suicide_6month_1year", "title": "六个月到一年"},
            {"payload": "/suicide_1year_2year", "title": "一年到两年"},
            {"payload": "/suicide_more_than_2year", "title": "两年以上"},
        ])
        if tracker.current_state()['events'][-2]['text'] == '/suicide_thought_yes':
            userid = tracker.current_state()['sender_id']
            sql = "update `user_info` set suicide_yes=1 where `id` ='" + userid + "';"
            common.connectmysql(sql)
        return []


class action_suicide_plan(Action):
    def name(self) -> Text:
        return "action_suicide_plan"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="有没有过关于自杀具体方法的思考？")
        return []


class action_suicide_behavior(Action):
    def name(self) -> Text:
        return "action_suicide_behavior"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        userid = tracker.current_state()['sender_id']
        conn = common.POOL.connection(shareable=False)
        cursor = conn.cursor()
        c = tracker.get_slot('suicide_plan_content')
        try:
            if c != None:
                # sql = "update `user_result` set topic_num=241,`user_scenes_intent`='/scence_one' where `user_info_id` ='" + userid + "' and `is_end` = 0 and `topic_type` = 0;"
                sql = "update `user_result` set `suicide_plan_contant`= '" + str(
                    c) + "' where `user_info_id` ='" + userid + "'and `is_end` = 0 and topic_type= 2 order by create_time desc limit 1"
                common.connectmysql(sql)
        except:
            dispatcher.utter_message(text=" ！！保存自杀方法不成功 ")
        finally:
            cursor.close()
            conn.close()
        dispatcher.utter_message(text="谢谢你跟我分享你的小秘密~")
        dispatcher.utter_message(text="那么你有实施过或者初步尝试过你设想的这些自杀的行为吗？", buttons=[
            {"payload": "/suicide_behavior_yes", "title": "有过……"},
            {"payload": "/suicide_behavior_no", "title": "没有"},
        ])
        return []


class action_last_3_state(Action):
    def name(self) -> Text:
        return "action_last_3_state"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:

        uptemp = tracker.get_slot('into_illness')
        if uptemp == "躁狂":
            # dispatcher.utter_message(text="双相状态或环型心境")
            text = "双相状态或环型心境"
        elif uptemp == "重性":
            # dispatcher.utter_message(text="重性抑郁状态")
            text = "重性抑郁状态"
        elif uptemp == "持续性":
            # dispatcher.utter_message(text='持续性抑郁状态（心境恶劣）')
            text = '持续性抑郁状态（心境恶劣）'
        else:
            print("我的诊断流程出错啦，出错啦")
        userid = tracker.current_state()['sender_id']
        common.connectredis(2, userid, 0)
        conn = common.POOL.connection(shareable=False)
        cursor = conn.cursor()
        now = time.localtime()
        try:
            # userid = tracker.current_state()['sender_id']
            y = "update `user_result` set level_of_depression_heart='" + text + "',is_end=1 ,end_time=now() where `user_info_id` ='" + userid + "'and is_end=0 and topic_type= 2 order by create_time desc limit 1;"

            common.connectmysql(y)
            cursor.close()
            conn.close()
            dispatcher.utter_message(text="你想要看看最终的诊断结果吗？", buttons=[
                {"payload": "/result_yes", "title": "想看想看！"},
                {"payload": "/result_no", "title": "不要……"},
            ])
        except:
            dispatcher.utter_message(text='诊断结果存储出错啦出错啦啊啊')
        return []


class health_end(Action):
    def name(self) -> Text:
        return "action_health_end"

    async def run(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="太棒啦！~你最近的情绪很平稳，请继续保持哦~"),
        dispatcher.utter_message(text='''请输入‘你好’，点击其他的功能跟小E继续互动吧~~''')
        return [SessionStarted()]
